/*
 * Main.fx
 *
 * Created on 1-dec-2009, 19:05:15
 */

package test;

/**
 * @author arne
 */

import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;

import javafx.scene.text.*;
import javafx.scene.control.Button;
import javafx.scene.Cursor;
import javafx.scene.input.*;
import javafx.scene.control.Slider;


def media = Media {
    source: "http://localhost/1460825906_2956241001_big-buck-bunny-640x360.flv"
};

var mediaPlayer : MediaPlayer = MediaPlayer {
        autoPlay: true //the media starts as soon as possible
        repeatCount: MediaPlayer.REPEAT_FOREVER //infinite time cycle for the media
        media: bind media
    }

var player : MediaView = MediaView {
    mediaPlayer: bind mediaPlayer;
    fitWidth: 300;
    cursor: Cursor.HAND

    onMouseClicked: function(me:MouseEvent) {
        if (mediaPlayer.paused){
            player.mediaPlayer.play();
        }
        else {
            player.mediaPlayer.pause();
        }
    }


};

def imagePlay = Image {url: "{__DIR__}play.jpg"};
def imagePause = Image {url: "{__DIR__}pause.jpg"};

var controlButton = ImageView {
    image: bind if (player.mediaPlayer.paused)
        then imagePlay
        else imagePause
    onMouseClicked: function (e) {
        if (player.mediaPlayer.paused){
        // resume or start playing if the player is paused
        player.mediaPlayer.play();
        }
        else {
        // pause playing if the player is run
        player.mediaPlayer.pause();
        }
    }

    cursor: Cursor.HAND

    x: 0
    y: 200
};

var movieProgress : Duration = bind player.mediaPlayer.currentTime;

var mvProgress : Number = player.mediaPlayer.currentTime.toMillis() on replace oldValue = newValue {
    player.mediaPlayer.currentTime = Duration.valueOf(newValue)
}




var slider = Slider {
    min: 0
    max: bind player.mediaPlayer.stopTime.toMillis()
    value: bind mvProgress with inverse

    width: 300

    layoutX: 100
    layoutY: 200
}

var timeline = Timeline {

    repeatCount: Timeline.INDEFINITE

    keyFrames: [
        KeyFrame {
            time: 0s
            action: function() {slider.value = player.mediaPlayer.currentTime.toMillis() }
        }

    ]

}

Stage {
    title: "JavaFX"
    scene: Scene {
        width: 600
        height: 400
        content: [
            player,
            controlButton,
            slider,


            Text {
                content: bind "{%tM player.mediaPlayer.currentTime}:{%tS player.mediaPlayer.currentTime}"
                x: 30
                y: 200
            }


            Button {
                text: "Ga naar aftitteling"

                layoutX: 0;
                layoutY: 275;

                onMouseClicked: function(me:MouseEvent) {
                    player.mediaPlayer.currentTime =  8m + 8s;
                }

                cursor: Cursor.HAND;
            }
        ]
    }
}